package com.example.wan1.view.mine;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.wan1.R;
import com.example.wan1.adapter.ItemAdapter;
import com.example.wan1.base.fragment.BaseFragment;
import com.example.wan1.bus.RxBus;
import com.example.wan1.event.LoginEvent;
import com.example.wan1.interfaces.RvScrollListener;
import com.example.wan1.model.ItemBean;
import com.example.wan1.model.UserBean;
import com.example.wan1.utils.ToolsUtils;
import com.example.wan1.view.MainActivity;
import com.example.wan1.view.coin.CoinFragment;
import com.example.wan1.view.coin.CoinRankFragment;
import com.example.wan1.view.collect.CollectActivity;
import com.example.wan1.view.login.LoginActivity;
import com.example.wan1.view.login.UserManager;
import com.example.wan1.view.setting.SettingDialogFragment;
import com.example.wan1.view.todo.TodoActivity;
import com.example.wan1.view.user.UserCenterActivity;
import com.example.wan1.view.web.JumpTool;
import com.example.wan1.view.wechat.WechatActivity;
import com.example.wan1.widget.CircleImageView;

import java.util.List;

import javax.inject.Inject;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;
import dagger.android.support.AndroidSupportInjection;

/***
 * @Author: zxd
 * @CreateDate: 2020/6/29 10:43
 * @Description: 我的
 * 当我的界面RecycleView的item增多的时候，界面放不下了，就可以让其进行滑动，然后关联fab，形成联动效果
 */
public class MineFragment extends BaseFragment {

    @BindView(R.id.wechat)
    LinearLayout mWechat;
    @BindView(R.id.mycollect)
    LinearLayout mMycollect;
    @BindView(R.id.myshare)
    LinearLayout mMyshare;
    @BindView(R.id.todo)
    LinearLayout mTodo;
    @BindView(R.id.mine_avatar_container)
    RelativeLayout mAvatar;
    @BindView(R.id.mine_user)
    TextView mUser;
    @BindView(R.id.mine_user_infor)
    TextView mUserInfo;
    @BindView(R.id.mine_rv)
    RecyclerView mMineRv;
    @BindView(R.id.mine_avatar_img)
    CircleImageView mCircleImageView;
    @Inject
    ItemAdapter mAdapter;
    @Inject
    List<ItemBean> mList;
    @BindView(R.id.fake_status_bar)
    View mFakeStatusBar;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        AndroidSupportInjection.inject(this);
        super.onCreate(savedInstanceState);
    }

    //碎片对话框
    private SettingDialogFragment mSettingDialogFragment;
    private CoinFragment mCoinFragment;
    private CoinRankFragment mCoinRankFragment;

    private String[] mTitles = new String[]{"积分排行榜", "我的积分", "设置"};
//    private String[] mTitles = new String[]{"积分排行榜", "我的积分", "设置", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"};

    @Override
    protected int initLayoutRes() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        mAvatar.setOnClickListener(v -> {
            if (TextUtils.equals("请登录", mUser.getText().toString())) {
                //去登陆或者注册
                startActivity(new Intent(mContext, LoginActivity.class));
            } else {
                //如果已经登录，那么就去用户中心的界面
                //todo 去用户界面
                Intent intent = new Intent(mContext, UserCenterActivity.class);
                intent.putExtra("id", -1);
                startActivity(intent);
            }
        });

        //mList = new ArrayList<>();
        initItemData();
        //mAdapter = new ItemAdapter(R.layout.mine_item, mList);
        mMineRv.setAdapter(mAdapter);
        //https://blog.csdn.net/wuqingsen1/article/details/83106886 android recycleview禁止滑动
        mMineRv.setLayoutManager(new LinearLayoutManager(mContext) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        //mMineRv.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        mAdapter.setOnItemClickListener((baseQuickAdapter, view, i) -> {
            /*if (mList.get(i).getTitle().equals(mTitles[0]))
                CoinRankFragment.newInstance("").show(getChildFragmentManager(), "dialog_fragment1");
            if (mList.get(i).getTitle().equals(mTitles[1]))
                CoinFragment.newInstance("").show(getChildFragmentManager(), "dialog_fragment2");
            if (mList.get(i).getTitle().equals(mTitles[2]))
                SettingDialogFragment.newInstance("").show(getChildFragmentManager(), "setting_dialog_fragment");*/

            if (mList.get(i).getTitle().equals(mTitles[0])) {
                if (mCoinRankFragment == null)
                    mCoinRankFragment = CoinRankFragment.newInstance("");
                /*if (mCoinRankFragment.isAdded()) {
                    mCoinRankFragment.dismiss();
                }*/
                mCoinRankFragment.show(getChildFragmentManager(), "dialog_fragment1");
            }
            if (mList.get(i).getTitle().equals(mTitles[1])) {
//                if (!AppUtils.isLogin()) {
                if (!UserManager.getInstance().isLogin()) {
                    JumpTool.goLogin(getActivity());
                    return;
                }
                if (mCoinFragment == null)
                    mCoinFragment = CoinFragment.newInstance("");
                mCoinFragment.show(getChildFragmentManager(), "dialog_fragment2");
            }
            if (mList.get(i).getTitle().equals(mTitles[2])) {
                if (mSettingDialogFragment == null)
                    mSettingDialogFragment = SettingDialogFragment.newInstance("");
                mSettingDialogFragment.show(getChildFragmentManager(), "setting_dialog_fragment");
            }
        });
        /*int color = ToolsUtils.getThemeColor(getActivity());
        if (color != 0) {
            mCircleImageView.setBorderColor(color);
        }*/

        //滑动隐藏tab
        if (getActivity() instanceof MainActivity) {
            mRvScrollListener = (RvScrollListener) getActivity();
        }
        mMineRv.addOnScrollListener(mOnScrollListener);
    }

    private void initItemData() {
        ItemBean bean;
        for (int i = 0; i < mTitles.length; i++) {
            bean = new ItemBean();
            bean.setTitle(mTitles[i]);
            bean.setType(1);
            bean.setCheck(false);
            bean.setSeek(false);
            mList.add(bean);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //设置我的信息
        if (UserManager.getInstance().isLogin()) {
            UserBean user = UserManager.getInstance().getUser();
            mUser.setText(user.getUsername());
            mUserInfo.setText("积分 " + user.getCoinCount() + " 等级 " + user.getLevel() + "");
        } else {
            mUserInfo.setText("请点击头像登录");
        }

        RxBus.getDefault().subscribe(mContext, new RxBus.Callback<LoginEvent>() {
            @Override
            public void onEvent(LoginEvent loginEvent) {
                if (loginEvent.getMsg().equals("1")) {
                    Log.i("zxd", "onEvent: ");
                    UserBean user = UserManager.getInstance().getUser();
                    mUser.setText(user.getUsername());
                    mUserInfo.setText("积分 " + user.getCoinCount() + " 等级 " + user.getLevel() + "");
                }
            }
        });
        /*RxBus.getDefault().subscribe(mContext, new RxBus.Callback<String>() {
            @Override
            public void onEvent(String s) {
                if (s.equals("theme_color")) {
                    int color = ToolsUtils.getThemeColor(getActivity());
                    mCircleImageView.setBorderColor(color);
                }
            }
        });*/
    }

    @OnClick({R.id.wechat, R.id.mycollect, R.id.myshare, R.id.todo})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.wechat:
                //这里使用Activity，不使用碎片了，等到换到下一个版本的时候在使用碎片
                startActivity(new Intent(getActivity(), WechatActivity.class));
                break;
            case R.id.mycollect:
                if (!UserManager.getInstance().isLogin()) {
                    JumpTool.goLogin(getActivity());
                    return;
                }
                intent = new Intent(mContext, CollectActivity.class);
                intent.putExtra("type", "my_collect");
                startActivity(intent);
                break;
            case R.id.myshare:
                if (!UserManager.getInstance().isLogin()) {
                    JumpTool.goLogin(getActivity());
                    return;
                }
                intent = new Intent(mContext, CollectActivity.class);
                intent.putExtra("type", "my_share");
                startActivity(intent);
                break;
            case R.id.todo:
                if (!UserManager.getInstance().isLogin()) {
                    JumpTool.goLogin(getActivity());
                    return;
                }
                intent = new Intent(mContext, TodoActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.getDefault().unregister(mContext);
    }

    public void setTvTitleBackgroundColor(int color) {
        if (mFakeStatusBar == null) return;
        mFakeStatusBar.setBackgroundColor(color);
    }
}
